package com.yiyuan.app.config;

import com.guaike.core.web.filter.AccessLogFilter;
import com.guaike.core.web.filter.CheckSignFilter;
import com.guaike.core.web.filter.CrossFilter;
import com.yiyuan.app.dao.OAppVersionDAO;
import com.yiyuan.app.filter.AppUpdateFilter;
import com.yiyuan.app.filter.CheckUseAppFilter;
import com.yiyuan.app.filter.LoginFilter;
import com.yiyuan.app.service.BaseUtilService;
import lombok.AllArgsConstructor;
import org.redisson.api.RedissonClient;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

/**
 * @Author: zhangxilin
 * @Date: 2024/9/25
 */
@Configuration
@AllArgsConstructor
public class FilterConfig {

    private final EnvironmentConfig environmentConfig;
    private final RedissonClient redissonClient;
    private final OAppVersionDAO oAppVersionDAO;
    private final BaseUtilService baseUtilService;

    @Bean
    public FilterRegistrationBean<CrossFilter> crossFilter() {
        FilterRegistrationBean<CrossFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        CrossFilter crossFilter = new CrossFilter();
        filterRegistrationBean.setFilter(crossFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("crossFilter");
        filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<AccessLogFilter> accessLogFilter() {
        FilterRegistrationBean<AccessLogFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        AccessLogFilter accessLogFilter = new AccessLogFilter();
        filterRegistrationBean.setFilter(accessLogFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("accessLogFilter");
        filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 2);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<CheckSignFilter> checkSignFilter() {
        FilterRegistrationBean<CheckSignFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        CheckSignFilter checkSignFilter = new CheckSignFilter();
        checkSignFilter.setEnv(environmentConfig.getActive());
        filterRegistrationBean.setFilter(checkSignFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("checkSignFilter");
        filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 3);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<AppUpdateFilter> appUpdateFilter() {
        FilterRegistrationBean<AppUpdateFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        AppUpdateFilter appUpdateFilter = new AppUpdateFilter();
        appUpdateFilter.setoAppVersionDAO(oAppVersionDAO);
        filterRegistrationBean.setFilter(appUpdateFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("appUpdateFilter");
        filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 4);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<LoginFilter> loginFilter() {
        FilterRegistrationBean<LoginFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        LoginFilter loginFilter = new LoginFilter();
        loginFilter.setRedissonClient(redissonClient);
        filterRegistrationBean.setFilter(loginFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("loginFilter");
        filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 5);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<CheckUseAppFilter> checkUseAppFilter() {
        FilterRegistrationBean<CheckUseAppFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        CheckUseAppFilter checkUseAppFilter = new CheckUseAppFilter();
        checkUseAppFilter.setRedissonClient(redissonClient);
        checkUseAppFilter.setBaseUtilService(baseUtilService);
        filterRegistrationBean.setFilter(checkUseAppFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("checkUseAppFilter");
        filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 6);
        return filterRegistrationBean;
    }

}
